<!DOCTYPE html>
<meta charset="utf-8">
<title>Worklet: addModule() on a detached iframe</title>
<body>
</body>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>

function with_iframe(url) {
  return new Promise(resolve => {
      let frame = document.createElement('iframe');
      frame.src = url;
      frame.onload = () => { resolve(frame); };
      document.body.appendChild(frame);
      add_result_callback(() => frame.remove());
    });
}

// This test should not be upstreamed to WPT because the spec does not define
// behavior in the case where addModule() is called from a detached frame.
promise_test(t => {
  const kFrameUrl = 'resources/blank.html';
  const kScriptUrl = 'resources/empty-worklet-script.js';

  return with_iframe(kFrameUrl)
    .then(frame => {
        let worklet = frame.contentWindow.CSS.paintWorklet;
        frame.remove();
        return worklet.addModule(kScriptUrl);
      })
    .then(() => assert_unreached('addModule() should fail.'))
    .catch(e => assert_equals(e.name, 'InvalidStateError', e));
}, 'addModule() on a detached iframe should be rejected.');

</script>
